<?php
namespace app\daifast\controller;
use \think\Db;
use think\Session;


class Article extends \think\Controller
{ 
    
    
    
    /**
     * 文章列表
     */
    public function index(){
         
        $title = addslashes(input('title')); 
        $category_id = input('category_id');
        //获取文章分类
        $cArr = Db('baode_article_category')->where(" pid >0 ")->select();
        $categoryArr = array_column($cArr, 'title','id');
        
        
        //查询条件
        $where = " category_id not in (1,2) ";
        $title and $where .=" and title like '%".$title."%'"; 
        $category_id and $where .=" and category_id = ".intval($category_id);
       
        $pageQueryArr =["query"=>["title"=>$title,"category_id"=>$category_id]];
        $data = Db('baode_article')->where($where)->order("id desc")->paginate(10,$pageQueryArr); 
        $listArr = $data->items();
        $pages = $data->render(); 
        
        return $this->allFetch(get_defined_vars());
    }
     
    /**
     * 删除
     */
    public function delete(){
        $aid = intval(input('aid'));
        
        Db('baode_article')->where(['id'=>$aid])->delete();
        
        $this->showMsg('删除成功', url('Article/index'));
        
    }
    
    
    /**
     * 添加文章
     * @return mixed
     */
    public function add(){
      //获取文章分类
      $cArr = Db('baode_article_category')->where("id not in (1,2)")->select();
      $categoryArr = array_column($cArr, 'title','id');
        
      $aid = intval(input('aid'));
      $aid and $ArticleInfo = Db('baode_article')->where(['id'=>$aid])->find();
        if(input('sbt')){ 
            $data['title'] = input('title');
            $data['category_id'] = input('category_id');
            $data['source'] = input('source');
            $data['author'] = input('author');
            $data['views'] = input('views');
            $data['is_show'] = input('is_show'); 
            $data['is_top'] = input('is_top');
            $data['keywords'] = input('keywords');
            $data['description'] = input('description');
            $data['logo'] = input('logo'); 
            $data['content'] = htmlspecialchars(input('content'));
            $data['on_time'] = time(); 
            
            
            $aid and Db('baode_article')->where(['id'=>$aid])->update($data); 
            $aid or Db('baode_article')->insert($data); 
            $this->showMsg('操作成功', url('Article/index'));
        }
        
        return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 文章分类管理
     */
    public function category(){
         
        $listArr = Db('baode_article_category')->where(['pid'=>0])->order("id desc")->select(); 
        $allArr = Db('baode_article_category')->where("pid>0")->order("id desc")->select();
        foreach ($allArr as $k=>$v){
            $sonList[$v[pid]][] =  $v;
            
        }  
        return $this->allFetch(get_defined_vars());
    }
    
    
    
    /**
     * 文章分类添加
     */
    public function categoryAdd(){
        $pid = input('pid');
        $aid = input('aid');
        $title = input('title');
        //获取所有父类
        $cArr = Db('baode_article_category')->where(['pid'=>0])->order("id desc")->select();
        $categoryArr = array_column($cArr, 'title','id');
        $aid and $categoryInfo = Db('baode_article_category')->where(['id'=>$aid])->find();
        
        if(input('sbt')){
            $pid == "top" and $pid = 0 or $pid = intval($pid);
            $data ['pid'] = $pid;
            $data ['title'] = $title;
            $data ['on_time'] = time();
            $aid and Db('baode_article_category')->where(['id'=>$aid])->update($data);
            $aid or Db('baode_article_category')->insert($data);
            $this->showMsg('操作成功', url('Article/category'));
            
        }
        
        
        return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 文章分类删除
     */
    public function categorydelete(){
        
        $aid = input('aid');
      
        Db('baode_article_category')->where("id = ".intval($aid)." or pid=".intval($aid))->delete();
        
        $this->showMsg('删除成功', url('Article/category'));
        
        
    }
    
    
    
    
    
    
}
